package giteeai.container;

import cc.git.liuyan.customeraiagent.core.bigmodel.BigModel;
import cc.git.liuyan.customeraiagent.core.bigmodel.BigModelProvider;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.EmbeddingModel;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.EmbeddingModelProvider;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.HybridEmbeddingModel;
import cc.git.liuyan.customeraiagent.core.embeddingmodel.SimpleEmbeddingModelSplit;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.api.interceptor.ApiKeyHeaderInterceptor;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.api.interceptor.ReqLogAndResLogInterceptor;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.bigmodel.GiteeAiBigModelProvider;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.bigmodel.GiteeAiBigModelProviderInfo;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.embeddingmodel.GiteeAiEmbeddingModelProvider;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.embeddingmodel.GiteeAiEmbeddingModelProviderInfo;
import cc.git.liuyan.customeraiagent.core.provider.giteeai.embeddingmodel.hybrid.HybridGiteeAiEmbeddingModel;
import cc.git.liuyan.customeraiagent.core.suppertool.KnowledgeSupperTool;
import cc.git.liuyan.customeraiagent.core.suppertool.Text2SqlSupperTool;
import cc.git.liuyan.customeraiagent.core.vectordatabase.MilvusVectorDataBase;
import cc.git.liuyan.customeraiagent.core.vectordatabase.VectorDataBase;
import giteeai.impl.SimpleBigModelChatTool;
import okhttp3.Dispatcher;
import okhttp3.Interceptor;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class Container {
    public static VectorDataBase vectorDataBase;
    public static EmbeddingModelProvider embeddingModelProvider;
    public static BigModelProvider bigModelProvider;
    public static EmbeddingModel denseEmbeddingModel;
    public static EmbeddingModel sparseEmbeddingModel;
    public static HybridEmbeddingModel hybridEmbeddingModel;

    public static BigModel chatBigModel;
    public static BigModel text2SqlBigModel;

    public Container() {
        //向量数据库初始化 链接地址+密钥token  免费的经不起折腾 会随时删除
        //自己的向量库需要初始化几张表，参考MilvusVectorDataBase里的表和字段
        vectorDataBase = new MilvusVectorDataBase("https://in03-97b10f101f334b3.serverless.ali-cn-hangzhou.cloud.zilliz.com.cn", "34b0ea8dfe0b9727327ddac8e6c50ff7eebfc105a2a65b061de106d8e57d2c0783f70e57c182f367400e1059e4164489487635df");

        //内嵌模型提供商初始化  提供商用于提供各种模型
        {
            //提供商可配置参数
            Dispatcher dispatcher = new Dispatcher();
            dispatcher.setMaxRequests(50);
            dispatcher.setMaxRequestsPerHost(10);
            GiteeAiEmbeddingModelProviderInfo embeddingProviderInfo = new GiteeAiEmbeddingModelProviderInfo();
            embeddingProviderInfo.setApiCallTimeout(Duration.ofSeconds(60));
            embeddingProviderInfo.setApiConnectTimeout(Duration.ofSeconds(60));
            embeddingProviderInfo.setApiReadTimeout(Duration.ofSeconds(60));
            embeddingProviderInfo.setApiWriteTimeout(Duration.ofSeconds(60));
            embeddingProviderInfo.setApiDispatcher(dispatcher);
            embeddingProviderInfo.setApiProxy(null);
            embeddingProviderInfo.setBaseUrl("https://ai.gitee.com/v1/"); //giteeai提供商就这个url不用变  其它提供商自己实现
            embeddingProviderInfo.setApiKey("TCRYGOLLXMSWJ6NIWKQYSKFPNNSI2OUYBY66J3BF"); //自行替换
            List<Interceptor> interceptors1 = new ArrayList<>();
            interceptors1.add(new ApiKeyHeaderInterceptor(embeddingProviderInfo.getApiKey()));
            interceptors1.add(new ReqLogAndResLogInterceptor());
            embeddingProviderInfo.setApiInterceptors(interceptors1);
            //根据提供商可配置参数 实现1个提供商
            embeddingModelProvider = new GiteeAiEmbeddingModelProvider(embeddingProviderInfo);
        }

        //初始化内嵌模型
        denseEmbeddingModel = embeddingModelProvider.getEmbeddingModel("bce-embedding-base_v1");
        denseEmbeddingModel.setSplit(new SimpleEmbeddingModelSplit());
        sparseEmbeddingModel = embeddingModelProvider.getEmbeddingModel("bge-m3");
        sparseEmbeddingModel.setSplit(new SimpleEmbeddingModelSplit());
        hybridEmbeddingModel = new HybridGiteeAiEmbeddingModel(denseEmbeddingModel, sparseEmbeddingModel);

        //大模型提供商初始化  提供商用于提供各种模型
        {
            Dispatcher dispatcher = new Dispatcher();
            dispatcher.setMaxRequests(50);
            dispatcher.setMaxRequestsPerHost(10);
            GiteeAiBigModelProviderInfo bigModelProviderInfo = new GiteeAiBigModelProviderInfo();
            bigModelProviderInfo.setApiCallTimeout(Duration.ofSeconds(60));
            bigModelProviderInfo.setApiConnectTimeout(Duration.ofSeconds(60));
            bigModelProviderInfo.setApiReadTimeout(Duration.ofSeconds(60));
            bigModelProviderInfo.setApiWriteTimeout(Duration.ofSeconds(60));
            bigModelProviderInfo.setApiDispatcher(dispatcher);
            bigModelProviderInfo.setApiProxy(null);
            bigModelProviderInfo.setLogStreamingResponses(false);
            bigModelProviderInfo.setBaseUrl("https://ai.gitee.com/v1/");
            bigModelProviderInfo.setApiKey("TCRYGOLLXMSWJ6NIWKQYSKFPNNSI2OUYBY66J3BF");
            List<Interceptor> interceptors2 = new ArrayList<>();
            interceptors2.add(new ApiKeyHeaderInterceptor(bigModelProviderInfo.getApiKey()));
            interceptors2.add(new ReqLogAndResLogInterceptor());
            bigModelProviderInfo.setApiInterceptors(interceptors2);
            bigModelProvider = new GiteeAiBigModelProvider(bigModelProviderInfo);
        }

        //初始化1个聊天对话大模型
        chatBigModel = bigModelProvider.getBigModel("DeepSeek-V3");
        chatBigModel.setChatTool(new SimpleBigModelChatTool());

        //初始化1个TeXT2SQL大模型(其实还是聊天对话大模型)
        text2SqlBigModel = bigModelProvider.getBigModel("DeepSeek-V3");
        text2SqlBigModel.setChatTool(new SimpleBigModelChatTool());

        //初始化超级工具
        KnowledgeSupperTool.init(hybridEmbeddingModel, vectorDataBase);
        Text2SqlSupperTool.init(hybridEmbeddingModel, vectorDataBase);
    }
}
